<?php
/*
本系统由苏酷伊基于SiteHub项目二次开发
作者博客： https://www.sukuy.com/
后续更新地址： https://www.sukuy.com/navigation
作者QQ：3288637559
QQ交流群：884250547
任何第三方非官网在发布本系统时，不得删除该注释、版权信息、作者信息及官网等内容
*/
define('IN_ADMIN', true);
include("../includes/common.php");
if ($admin_islogin == 1) {
} else exit("<script language='javascript'>window.location.href='./login.php';</script>");

$act = _get('act');
switch ($act) {
    case 'add':
        $sid      = _post('sid');
        $icon     = _post('icon');
        $catename = _post('catename');
        $alias    = _post('alias');
        $count_article_category = $DB->count('article_category', array('catename' => $catename));
        if ($count_article_category) {
            
            add_action_log($conf['admin_user'], '添加文章分类失败：分类名不可重复', 0);
            exit("<script>alert('分类名不可重复');history.go(-1)</script>");
        }
        if ($alias) {
            $count_alias = $DB->count('article_category', array('alias' => $alias));
            if ($count_alias) {
                
                add_action_log($conf['admin_user'], '添加文章分类失败：别名不可重复', 0);
                exit("<script>alert('别名不可重复！');history.go(-1)</script>");
            }
        }
        $result = $DB->insert('article_category', array(
            'sid'      => $sid,
            'icon'     => $icon,
            'catename' => $catename,
            'alias'    => $alias
        ));
        if ($result) {
            
            add_action_log($conf['admin_user'], '添加文章分类：' . $catename, 1);
            exit("<script>alert('添加成功！');location='./article_category.php';</script>");
        } else {
            
            add_action_log($conf['admin_user'], '添加文章分类失败：' . $catename, 0);
            exit("<script>alert('添加失败！');history.go(-1)</script>");
        }
        break;
    case 'edit':
        $id       = _post('id');
        $sid      = _post('sid');
        $icon     = _post('icon');
        $catename = _post('catename');
        $alias    = _post('alias');
        $row      = $DB->find('article_category', '*', array('id' => $id));
        if (!$row) {
            
            add_action_log($conf['admin_user'], '编辑文章分类失败(ID: ' . $id . ')：分类不存在', 0);
            exit("<script>alert('分类不存在！');location='./article_category.php';</script>");
        }
        if ($alias) {
            $count_alias = $DB->count('article_category', "`alias`='$alias' AND `id`<>$id");
            if ($count_alias) {
                
                add_action_log($conf['admin_user'], '编辑文章分类失败(ID: ' . $id . ')：别名不可重复', 0);
                exit("<script>alert('别名不可重复！');history.go(-1)</script>");
            }
        }
        
        $update_data = array(
            'sid'      => $sid,
            'icon'     => $icon,
            'catename' => $catename,
            'alias'    => $alias
        );
        $result = $DB->update('article_category', $update_data, array('id' => $id));
        if ($result) {
            
            add_action_log($conf['admin_user'], '编辑文章分类(ID: ' . $id . ')：' . $row['catename'] . ' -> ' . $catename, 1);
            if ($row['catename'] != $catename) {
                
                $result = $DB->update('article', array('catename' => $catename), array('catename' => $row['catename']));
                if ($result) {
                    
                    add_action_log($conf['admin_user'], '更新文章分类名：' . $row['catename'] . ' -> ' . $catename, 1);
                }
            }
            exit("<script>alert('修改成功！');location='./article_category.php';</script>");
        } else {
            
            add_action_log($conf['admin_user'], '编辑文章分类失败(ID: ' . $id . ')：' . $row['catename'], 0);
            exit("<script>alert('修改失败！');history.go(-1)</script>");
        }
        break;
    case 'del':
        @header('Content-Type: application/json; charset=UTF-8');
        $id = _post('id');
        if (!$id) {
            exit('{"code":-1,"msg":"ID不能为空！"}');
        }
        $row = $DB->find('article_category', 'catename', array('id' => $id));
        if (!$row) {
            
            add_action_log($conf['admin_user'], '删除文章分类失败(ID: ' . $id . ')：分类不存在', 0);
            exit('{"code":-1,"msg":"分类不存在！"}');
        }
        $catename = $row['catename'];
        
        $article_count = $DB->count('article', array('catename' => $catename));
        if ($article_count > 0) {
            
            add_action_log($conf['admin_user'], '删除文章分类失败(ID: ' . $id . ')：' . $catename . '（该分类下存在文章）', 0);
            exit('{"code":-1,"msg":"该分类下存在文章，无法删除！"}');
        }
        $result = $DB->delete('article_category', array('id' => $id));
        if ($result) {
            
            add_action_log($conf['admin_user'], '删除文章分类(ID: ' . $id . ')：' . $catename, 1);
            exit('{"code":0,"msg":"删除成功！"}');
        }
        
        add_action_log($conf['admin_user'], '删除文章分类失败(ID: ' . $id . ')：' . $catename, 0);
        exit('{"code":-1,"msg":"分类不存在！"}');
        break;
    default:
        @header('Content-Type: application/json; charset=UTF-8');
        exit('{"code":-4,"msg":"No Act"}');
        break;
}
